package com.wuzheng.wisgo.sanitation.cloud.controller.vehicleInfo;


import com.wuzheng.wisgo.sanitation.cloud.model.vehicleInfo.BaVehicleInfo;
import com.wuzheng.wisgo.sanitation.cloud.model.vehicleInfo.EcharsShowDataSelectVehInfoDto;
import com.wuzheng.wisgo.sanitation.cloud.paraandgetutil.VehicleException;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.vehicle.BaVehicle;
import com.wuzheng.wisgo.sanitation.cloud.service.vehicleInfo.VehicleInfoService;
import com.wuzheng.wisgo.sanitation.framework.base.controller.BaseController;
import com.wuzheng.wisgo.sanitation.framework.core.protocol.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/*
 * @Description: 车辆详情
 * @Author: sunpeng
 * @Date: 2019/12/4
**/
@RestController
@RequestMapping("/vehicleinfo")
@Api(value = "VehicleInfoCtrl", tags = "车辆信息接口")
public class VehicleInfoCtrl extends BaseController {

    @Autowired
    private VehicleInfoService vehicleInfoService;

    /**
     * 获取车辆信息列表
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "获取车辆信息列表")
    @ResponseBody
    public Result<BaVehicle> list(@RequestBody BaVehicleInfo baVehicleInfo)  {
        BaVehicle vehInfoToBase = null;
        try {
            vehInfoToBase = vehicleInfoService.getVehInfoToBase(baVehicleInfo.getVehVin());
        } catch (VehicleException e) {
            e.printStackTrace();
            this.failure(e.getMessage());
        }
        return this.success(vehInfoToBase);
    }

    /**
     * 获取车辆里程数据
     * （20191223，自定义为空码，请不要修改，如需修改麻烦联系开发人员）
     */
    @PostMapping(value = "/selectrectotalmiles")
    @ApiOperation(value = "获取车辆里程数据")
    @ResponseBody
    public Result<List<EcharsShowDataSelectVehInfoDto>> selectRecTotalMiles(@RequestBody BaVehicleInfo baVehicleInfo)  {
        List<EcharsShowDataSelectVehInfoDto> list = null;
        try {
            list = vehicleInfoService.getRecTotalMilesFromDay(baVehicleInfo.getVehVin(),baVehicleInfo.getStartTime(),baVehicleInfo.getStopTime());
            Boolean flag = this.checkDataOrNull(list,"1");
            if(flag){
                return this.success(list);
            }
        } catch (VehicleException e) {
            e.printStackTrace();
            this.failure(e.getMessage());
        }

        return this.success("20191223",list);
    }

    /**
     * 获取车辆作业里程数据
     * （20191223，自定义为空码，请不要修改，如需修改麻烦联系开发人员）
     */
    @PostMapping(value = "/selectworktotalmiles")
    @ApiOperation(value = "获取车辆作业里程数据")
    @ResponseBody
    public Result<List<EcharsShowDataSelectVehInfoDto>> selectWorkRecTotalMiles(@RequestBody BaVehicleInfo baVehicleInfo)  {
        List<EcharsShowDataSelectVehInfoDto> list = null;
        try {
            list = vehicleInfoService.getWorkRecTotalMiles(baVehicleInfo.getVehVin(),baVehicleInfo.getStartTime(),baVehicleInfo.getStopTime());
            Boolean flag = this.checkDataOrNull(list,"2");
            if(flag){
                return this.success(list);
            }
        } catch (VehicleException e) {
            e.printStackTrace();
            this.failure(e.getMessage());
        }
        return this.success("20191223",list);
    }

    /**
     * 获取车辆油耗数据
     * （20191223，自定义为空码，请不要修改，如需修改麻烦联系开发人员）
     */
    @PostMapping(value = "/selectrecfuelquantity")
    @ApiOperation(value = "获取车辆油耗数据")
    @ResponseBody
    public Result<List<EcharsShowDataSelectVehInfoDto>> selectRecFuelQuantity(@RequestBody BaVehicleInfo baVehicleInfo)  {
        List<EcharsShowDataSelectVehInfoDto> list = null;
        try {
            list = vehicleInfoService.getRecFuelQuantity(baVehicleInfo.getVehVin(),baVehicleInfo.getStartTime(),baVehicleInfo.getStopTime());
            Boolean flag = this.checkDataOrNull(list,"3");
            if(flag){
                return this.success(list);
            }
        } catch (VehicleException e) {
            e.printStackTrace();
            this.failure(e.getMessage());
        }
        return this.success("20191223",list);
    }

    /*
     * @Description: 判断是否存在数据不等于0的数据
     * @Author: sunpeng
     * @Date: 2019/12/23
    **/

    private Boolean checkDataOrNull(List<EcharsShowDataSelectVehInfoDto> list,String type){
        Boolean flag=false;
        for(EcharsShowDataSelectVehInfoDto echarsShowDataSelectVehInfoDto:list){
            //有一个数据不等于0，就说明查出的数据中有值
            switch(type){
                case "1" : //总里程(km）
                    if(echarsShowDataSelectVehInfoDto.getRecTotalMiles().compareTo(0D)!=0){
                        flag=true;
                    }
                    break;
                case "2" ://作业总里程(km
                    if(echarsShowDataSelectVehInfoDto.getWorkRecTotalMiles().compareTo(0D)!=0){
                        flag=true;
                    }
                    break;
                default : //默认油耗
                    if(echarsShowDataSelectVehInfoDto.getRecFuelQuantity().compareTo(0D)!=0){
                        flag=true;
                    }
            }
        }
        return  flag;
    }
}

